package com.itcast.donkey.fragments;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.itcast.donkey.R;
import com.itcast.donkey.activity.ToolBoxAreaActivity;
import com.itcast.donkey.activity.ToolMapActivity;
import com.itcast.donkey.activity.ToolRateActivity;
import com.itcast.donkey.activity.ToolTranslateActivity;
import com.itcast.donkey.activity.ToolTravelActivity;
import com.itcast.donkey.modles.ToolBoxTimeModles;
import com.itcast.donkey.modles.UriModles;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by lanouhn on 16/8/23.
 */
public class ToolBoxFragment extends Fragment implements View.OnClickListener {

    private LinearLayout linToolbaxArea;
    private TextView tvToolbaxArea;
    private TextView tvToolbaxMintemperature;
    private TextView tvToolbaxMaxtemperature;
    private TextView tvToolboxDay;
    private ImageView ivToolboxTranslate;
    private ImageView ivToolboxStar;
    private ImageView ivToolboxTravel;
    private ImageView ivToolboxMap;
    private ImageView ivToolboxRate;

    private ToolBoxTimeModles modles;
    private int id = 25;
    private String name = "";

    private SharedPreferences sp;
    private int areaID;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_toolbox, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        sp = getActivity().getSharedPreferences("config", Context.MODE_PRIVATE);
        id = sp.getInt("id", 23);
        name = sp.getString("name", "上海");
        areaID = sp.getInt("areaID", 55);

        initView(getView());
        initData();
        initEvent();
    }

    private void initView(View view) {
        linToolbaxArea = (LinearLayout) view.findViewById(R.id.lin_toolbax_area);
        tvToolbaxArea = (TextView) view.findViewById(R.id.tv_toolbax_area);
        tvToolbaxMintemperature = (TextView) view.findViewById(R.id.tv_toolbax_mintemperature);
        tvToolbaxMaxtemperature = (TextView) view.findViewById(R.id.tv_toolbax_maxtemperature);
        tvToolboxDay = (TextView) view.findViewById(R.id.tv_toolbox_day);
        ivToolboxTranslate = (ImageView) view.findViewById(R.id.iv_toolbox_translate);
        ivToolboxStar = (ImageView) view.findViewById(R.id.iv_toolbox_star);
        ivToolboxTravel = (ImageView) view.findViewById(R.id.iv_toolbox_travel);
        ivToolboxMap = (ImageView) view.findViewById(R.id.iv_toolbox_map);
        ivToolboxRate = (ImageView) view.findViewById(R.id.iv_toolbox_rate);
    }

    private void initData() {
        final String url = UriModles.TOOLBOX_FRONT_AREA_TIME_URL + id + UriModles.TOOLBOX_END_AREA_TIME_URL;
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(url)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        handler.sendEmptyMessage(0);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String values = response.body().string();
                        Gson gson = new Gson();
                        modles = gson.fromJson(values, ToolBoxTimeModles.class);
                        if (modles != null) {
                            handler.sendEmptyMessage(1);
                        } else {
                            handler.sendEmptyMessage(2);
                        }
                    }
                });
            }
        }).start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Toast.makeText(getActivity(), "获取网络失败", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    tvToolbaxMintemperature.setText(modles.getTemp_min() + "度");
                    tvToolbaxMaxtemperature.setText(modles.getTemp_max() + "度");
                    tvToolbaxArea.setText(name);
                    tvToolboxDay.setText("当地时间" + modles.getCurrent_time());
                    break;
                case 2:
                    Toast.makeText(getActivity(), "解析失败", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    private void initEvent() {
        tvToolbaxArea.setOnClickListener(this);
        ivToolboxTranslate.setOnClickListener(this);
        ivToolboxTravel.setOnClickListener(this);
        ivToolboxMap.setOnClickListener(this);
        ivToolboxRate.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.tv_toolbax_area:
                Intent intent = new Intent(getActivity(), ToolBoxAreaActivity.class);
                startActivity(intent);
                break;
            case R.id.iv_toolbox_translate:
                Intent intent1 = new Intent(getActivity(), ToolTranslateActivity.class);
                startActivity(intent1);
                break;
            case R.id.iv_toolbox_travel:
                Intent intent2 = new Intent(getActivity(), ToolTravelActivity.class);
                intent2.putExtra("currency_display", modles.getCurrency_display());
                startActivity(intent2);
                break;
            case R.id.iv_toolbox_map:
                Intent intent3 = new Intent(getActivity(), ToolMapActivity.class);
                intent3.putExtra("id", areaID);
                startActivity(intent3);
                break;
            case R.id.iv_toolbox_rate:
                Intent intent4 = new Intent(getActivity(), ToolRateActivity.class);
                intent4.putExtra("id", areaID);
                intent4.putExtra("currency_code", modles.getCurrency_code());
                intent4.putExtra("currency_display", modles.getCurrency_display());
                startActivity(intent4);
                break;
        }
    }
}
